package com.ysh.service.impl;

import com.ysh.bean.Student;
import com.ysh.mapper.StudentMapper;
import com.ysh.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    /*
        查询所有
     */
    @Override
    public List<Student> selectAll() {
        // 存储结果集合
        List<Student> list = null;
        // 实现类
        SqlSession sqlSession = null;
        // 字节输入流对象
        InputStream is = null;
        try {
            // 1. 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 2. 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 3. 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 4. 获取StudentMapper接口的实现类对象(代理对象)
            // 多态体现，实现类MyBatis动态生成实现类，等价于：StudentMapper mapper = new StudentMapperImpl();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            // 5. 通过实现类对象调用方法，接收结果
            list = mapper.selectAll();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 6. 释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 7.返回结果
        return list;
    }

    /*
        根据id进行查询
     */
    @Override
    public Student selectById(Integer id) {
        Student stu = null;
        SqlSession sqlSession = null;
        InputStream is = null;
        try {
            // 1. 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 2. 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 3. 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 4. 获取StudentMapper接口的实现类对象
            // 多态体现，等价于：StudentMapper mapper = new StudentMapperImpl();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            // 5. 通过实现类对象调用方法，接收结果
            stu = mapper.selectById(id);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 6. 释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 7.返回结果
        return stu;
    }

    /*
        新增方法
     */
    @Override
    public Integer insert(Student stu) {
        SqlSession sqlSession = null;
        InputStream is = null;
        Integer result = 0;
        try {
            // 1. 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 2. 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 3. 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 4. 获取StudentMapper接口的实现类对象
            // 多态体现，等价于：StudentMapper mapper = new StudentMapperImpl();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            // 5. 通过实现类对象调用方法，接收结果
            result = mapper.insert(stu);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 6. 释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 7.返回结果
        return result;
    }

    /*
        更新操作
     */
    @Override
    public Integer update(Student stu) {
        SqlSession sqlSession = null;
        InputStream is = null;
        Integer result = 0;
        try {
            // 1. 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 2. 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 3. 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 4. 获取StudentMapper接口的实现类对象
            // 多态体现，等价于：StudentMapper mapper = new StudentMapperImpl();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            // 5. 通过实现类对象调用方法，接收结果
            result = mapper.update(stu);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 6. 释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 7.返回结果
        return result;

    }

    /*
        删除操作
     */
    @Override
    public Integer delete(Integer id) {
        SqlSession sqlSession = null;
        InputStream is = null;
        Integer result = 0;
        try {
            // 1. 加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            // 2. 获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            // 3. 通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            // 4. 获取StudentMapper接口的实现类对象
            // 多态体现，等价于：StudentMapper mapper = new StudentMapperImpl();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

            // 5. 通过实现类对象调用方法，接收结果
            result = mapper.delete(id);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 6. 释放资源
            if (sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 7.返回结果
        return result;
    }

}
